package pers.karl.export.util;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;

/**
 * sql工具类
 * @author karl
 */
@Slf4j
public class SqlUtils {

	/**
	 * 建立连接
	 * @param url
	 * @param user
	 * @param password
	 * @return
	 */
	public static Connection getConnection(String url, String user, String password) throws SQLException {
		log.debug("get connection, url: {}", url);
		return DriverManager.getConnection(url, user, password);
	}

	/**
	 * 关闭连接
	 * @param conn
	 * @throws SQLException
	 */
	public static void closeConnection(Connection conn) throws SQLException {
		if(conn != null) {
			conn.close();			
		}
	}

	/**
	 * 获取结果集
	 * @param conn
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public static ResultSet getResultSet(Connection conn, String sql) throws SQLException {
		log.debug("query result, sql: {}", sql);
		Statement stat = conn.createStatement();
		return stat.executeQuery(sql);
	}

}
